# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst
from ..items import NovelItem


class XbiqugeSpider(scrapy.Spider):
    name = 'xbiquge'
    name_desc = "新笔趣阁"
    allowed_domains = ['www.xbiquge.la']
    start_urls = ['http://www.xbiquge.la/xiaoshuodaquan/']
    novel_info = ["novel_name", "novel_cover_url", "novel_desc", "novel_author", "novel_lasted_update",
                  "novel_lasted_chapter_name", "novel_lasted_chapter_url"]
    css_config = {
        "index_url_href": ".novellist a::attr(href)",
        "novel_name": "#info h1::text",
        "novel_cover_url": "#fmimg img::attr(src)",
        "novel_url": "",
        "novel_desc": "#intro p:nth-child(2)::text",
        "novel_author": '#info p:nth-child(2)::text',
        "novel_lasted_update": "#info p:nth-child(4)::text",
        "novel_lasted_chapter_name": "#info p:nth-child(5) a::text",
        "novel_lasted_chapter_url": "#info p:nth-child(5) a::attr(href)",
    }

    def parse(self, response):
        css_select = self.css_config['index_url_href']
        n_urls = response.css(css_select).extract()
        for n_url in n_urls:
            yield scrapy.Request(url=n_url, callback=self.parse_novel_info)

    def parse_novel_info(self, response):
        novel_loader = ItemLoader(item=NovelItem(), response=response)
        novel_loader.default_output_processor = TakeFirst()
        novel_loader.add_value("website_url", self.allowed_domains[0])
        novel_loader.add_value("novel_url", response.url)
        for novel_item in self.novel_info:
            novel_loader.add_css(novel_item, self.css_config[novel_item])
        novel_item = novel_loader.load_item()
        yield novel_item
